﻿/*********************************************************************************************************************
Unit tests VanguardDocumentation, Procedure SplitEntityType

© 2014 Erik Ackerholm

This file is part of SqlVanguard.

SqlVanguard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

SqlVanguard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with SqlVanguard. If not, see 
<http://www.gnu.org/licenses/>.
*********************************************************************************************************************/

set nocount on;
set xact_abort off;
GO

create table #TestTypes (
	TestNo              integer not null           identity (1, 1),
	FullEntityType      nvarchar(512),
	TypeLevel           tinyint,
	Level0Type          nvarchar(128),
	Level1Type          nvarchar(128),
	Level2Type          nvarchar(128),
	ErrorCode           integer,
	primary key (TestNo)
);
GO

insert into #TestTypes (FullEntityType, TypeLevel, Level0Type, Level1Type, Level2Type, ErrorCode)
values
	('Schema', 0, 'Schema', null, null, null),
	('Table', 1, null, 'Table', null, null),
	('Column', 2, null, null, 'Column', null),
	('Schema.Table', 1, 'Schema', 'Table', null, null),
	('Schema.Table', 2, null, null, null, 740900),
	('Schema.Table.Column', 2, 'Schema', 'Table', 'Column', null),
	('Table.Column', 2, null, 'Table', 'Column', null),
	('Type.With.Four.Parts', 2, null, null, null, 740900),
	('With.Three.Parts', 1, null, null, null, 740900),
	('Two.Parts', 0, null, null, null, 740900),
	(null, null, null, null, null, 740900),
	('', 0, null, null, null, null),
	('One very long Type without any dots 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890', 2, null, null, null, 740900);
GO

declare @TestNo as integer;
declare @FullEntityType as nvarchar(512);
declare @TypeLevel as tinyint;
declare @Level0Type as nvarchar(128);
declare @Level1Type as nvarchar(128);
declare @Level2Type as nvarchar(128);
declare @ErrorCode as integer;

set @TestNo = (select min(TestNo) from #TestTypes);

while
	@TestNo is not null
begin
	
	set @FullEntityType = (select FullEntityType from #TestTypes where TestNo = @TestNo);
	set @TypeLevel = (select TypeLevel from #TestTypes where TestNo = @TestNo);
	
	set @Level0Type = null;
	set @Level1Type = null;
	set @Level2Type = null;
	set @ErrorCode = null;
	
	begin try;
		execute VanguardDocumentation.SplitEntityType
			@FullEntityType, @TypeLevel, @Level0Type out, @Level1Type out, @Level2Type out;
	end try
	begin catch
		set @ErrorCode = error_number();
	end catch;

	if
		exists (
			select * from #TestTypes where TestNo = @TestNo
			except
			select @TestNo, @FullEntityType, @TypeLevel, @Level0Type, @Level1Type, @Level2Type, @ErrorCode
		)
	begin
		print 'SplitEntityType, Failed test no ' + rtrim(@TestNo);
		print '	' + coalesce(quotename(@FullEntityType, ''''), 'null')
			+ ', ' + coalesce(rtrim(@TypeLevel), 'null')
			+ ' >>> ' + coalesce(quotename(@Level0Type, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level1Type, ''''), 'null')
			+ ', ' + coalesce(quotename(@Level2Type, ''''), 'null')
			+ ' (Error ' + coalesce(rtrim(@ErrorCode), 'null') + ')';
	end;

	set @TestNo = (select min(TestNo) from #TestTypes where TestNo > @TestNo);

end;
GO

drop table #TestTypes;
GO

set nocount off;
GO

/*
-- To check a specific test:

declare @Level0Type as nvarchar(128);
declare @Level1Type as nvarchar(128);
declare @Level2Type as nvarchar(128);

execute VanguardDocumentation.SplitEntityType '.', 0, @Level0Type out, @Level0Type out, @Level0Type out;

select @Level0Type, @Level1Type, @Level2Type;
*/




